package model

import (
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	"log"
	"myGame/src/Config"
)

type Realm struct {
	Image1 []*ebiten.Image
	Width  []int
	Height []int
	X      float64
	Y      []float64
	Live   int
	State  int
	Dir    int
}

func NewRealm(screenWidth, screenHeight int) *Realm {
	img1, _, err := ebitenutil.NewImageFromFile("./src/images/rightRealm.png")
	if err != nil {
		log.Fatal(err)
	}
	img2, _, err := ebitenutil.NewImageFromFile("./src/images/xiadun.png")
	if err != nil {
		log.Fatal(err)
	}
	img3, _, err := ebitenutil.NewImageFromFile("./src/images/leftRealm.png")
	if err != nil {
		log.Fatal(err)
	}

	width1, height1 := img1.Size()
	//fmt.Println(width1, height1) 96  280
	width2, height2 := img2.Size()
	//fmt.Println(width2, height2)  113 150
	_, height3 := img3.Size()
	//fmt.Println(width2, height2)  96  280
	realm := &Realm{
		Image1: []*ebiten.Image{img1, img2, img3},
		Width:  []int{width1, width2},
		Height: []int{height1, height2},
		X:      100,
		Y:      []float64{float64((screenHeight - height1 - 130)), float64((screenHeight - height2 - 100)), float64((screenHeight - height3 - 130))},
		Live:   5,
		Dir:    1,
		State:  0,
	}
	return realm
}

// 给realman添加一个绘制自身的方法，传入屏幕对象screen和配置，让代码更好维护
func (realm *Realm) Draw(screen *ebiten.Image, cfg *Config.Config) {
	//这句代码是Ebiten游戏引擎提供的一个结构体类型，用于设置绘制图像时的选项和参数
	op := &ebiten.DrawImageOptions{}
	//fmt.Println(realm.X, realm.Y[realm.State])
	op.GeoM.Translate(realm.X, realm.Y[realm.State])

	//根据人物的面向选择应该画出哪一个人的照片
	if realm.Dir == 0 && realm.State == 0 {
		screen.DrawImage(realm.Image1[2], op)
	} else {
		screen.DrawImage(realm.Image1[realm.State], op)
	}

}
